package com.elderlyCare.controller.admin;

import java.util.Arrays;
import java.util.List;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;

import com.elderlyCare.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
public class modelOut {

    private String apiKey = "sk-c218c98ee4094f80acd346aed0ad1186"; // 将静态变量改为实例变量
    @Autowired
    private RestTemplate restTemplate;

    private static String TOKEN=
"MIIOdwYJKoZIhvcNAQcCoIIOaDCCDmQCAQExDTALBglghkgBZQMEAgEwggyJBgkqhkiG9w0BBwGgggx6BIIMdnsidG9rZW4iOnsiZXhwaXJlc19hdCI6IjIwMjQtMTItMjJUMDE6NDE6NDguOTU2MDAwWiIsIm1ldGhvZHMiOlsicGFzc3dvcmQiXSwiY2F0YWxvZyI6W10sInJvbGVzIjpbeyJuYW1lIjoidGVfYWRtaW4iLCJpZCI6IjAifSx7Im5hbWUiOiJ0ZV9hZ2VuY3kiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9jc2JzX3JlcF9hY2NlbGVyYXRpb24iLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9lY3NfZGlza0FjYyIsImlkIjoiMCJ9LHsibmFtZSI6Im9wX2dhdGVkX2Rzc19tb250aCIsImlkIjoiMCJ9LHsibmFtZSI6Im9wX2dhdGVkX29ic19kZWVwX2FyY2hpdmUiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9hX2NuLXNvdXRoLTRjI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Y40DbBsaZTzNAnNEkZ4GrhLG+DRHU-gIa3aRPNGfukCcjBv6SLBaS-l5NDi2e274eO9VVh21wsGTocGDkTxxJTLzJRmsthE3I5YwLlSGZF+ZTKUl2uwUP7iUowWCg02jmQmBGvAybrrUaRudXzbbnNa8WuNwlQo7LdQYy8X7MX7kBxTft2UESh4HsN2s="
    ;


    public String getDataWithToken() {
        String url = "https://3e5ac2e729.st1.iotda-app.cn-south-1.myhuaweicloud.com/v5/iot/954e4c6b28ab4040b1cbb71603d0bd60/devices/6753c82d61f92d003abc681d_dev1/shadow"; // 目标 URL

        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("X-Auth-Token",TOKEN); // 设置 Authorization 头，Bearer 类型的 Token

        // 创建请求实体，包含请求头
        HttpEntity<String> requestEntity = new HttpEntity<>(headers);

        // 发送 GET 请求
        ResponseEntity<String> response = restTemplate.exchange(
                url, HttpMethod.GET, requestEntity, String.class);
        System.out.println("Response Body: " + response.getBody());
        return response.getBody(); // 返回响应体
    }


    @RequestMapping("/admin/modelOut")
    public Result<String> getMessage() throws NoApiKeyException, InputRequiredException {

        Generation gen = new Generation();
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("你是一个对航天航空方面的专家系统,你很专业,我给你一些空间站的实时数据,请你以航天航空方面的专家系统生成一些日志,,请注意忽略AccX:23,AccY:-50,AccZ:1877,这些是错误数据,真正的加速度是total_acc,只要生成50字,只要给简短建议,Acc是加速度,"+getDataWithToken())
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content("")

                .build();
        GenerationParam param = GenerationParam.builder()
                .apiKey(apiKey)
                .model("qwen-turbo")//qwen-turbo//qwen-max//chatglm3-6b
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();

        GenerationResult result = gen.call(param);
        String content = result.getOutput().getChoices().get(0).getMessage().getContent();
        content = content.replace("#","");
        content = content.replace("*","");
        content = content.replace("-","");
        content = content.replace("\\n","");
        System.out.println(content);
        return Result.success(content);

    }
}
